package com.jch.fs.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.text.SimpleDateFormat;
import java.util.Date;

// Netty 中流量分块功能 | 对写入的大数据块 进行分块处理
// 使用 NetAssist.exe 网络调试助手测试 | 下载地址：https://www.cmsoft.cn/resource/102.html
public class FSServer {

    private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

    public static void main(String[] args) {
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();

        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(boss, worker)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 128)
                .handler(new LoggingHandler(LogLevel.DEBUG))
                .childHandler(new FSChannelInit());
        try {
            ChannelFuture channelFuture = bootstrap.bind(1136).sync();
            System.out.println(df.format(new Date()) + " fs server start done.");
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            System.out.println(df.format(new Date()) + " 异常：" + e.getMessage());
        } finally {
            System.out.println(df.format(new Date()) + " close fs server.");
            worker.shutdownGracefully();
            boss.shutdownGracefully();
        }
    }
}
